package com.pur.zsms.ordercustommanage.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.service.ICustMaterDetailedOrderService;
import com.pur.zsms.ordercustommanage.vo.OrderCustFoodVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.supplier.dao.SupplierMaterOrderDao;

@Service("custMaterDetailedOrderService")
public class CustMaterDetailedOrderServiceImpl implements ICustMaterDetailedOrderService {
	@Resource(name = "supplierMaterOrderDao")
	private SupplierMaterOrderDao supplierOrderDao;

	public SupplierMaterOrderDao getSupplierOrderDao() {
		return supplierOrderDao;
	}

	public void setSupplierOrderDao(SupplierMaterOrderDao supplierOrderDao) {
		this.supplierOrderDao = supplierOrderDao;
	}

	@Override
	public PaginationSupport listSupplierOrder(PageController pageController, OrderSupplierVo orderSupplierVo)
	        throws GenericException {
		return supplierOrderDao.listSupplierOrder(pageController, orderSupplierVo);
	}

	@Override
	public PaginationSupport listSupperOrderDetailed(PageController pageController,
	        OrderSupplierMaterVo orderSupplierVo, String oid) throws GenericException {
		return supplierOrderDao.listSupperOrderDetailed(pageController, orderSupplierVo, oid);
	}

	@Override
	public PaginationSupport listMaterToMaterDetailed(PageController pageController,
	        OrderSupplierMaterVo orderSupplierVo, String oid) throws GenericException {
		return supplierOrderDao.listMaterToMaterDetailed(pageController, orderSupplierVo, oid);
	}

	@Override
	public PaginationSupport findMatertoMaterDetailedReal(PageController pageController,
	        OrderSupplierMaterVo orderSupplierVo, String oid) throws GenericException {
		return supplierOrderDao.findMatertoMaterDetailedReal(pageController, orderSupplierVo, oid);
	}

	@Override
	public PaginationSupport listSelectMaterDetailed(PageController pageController,
	        OrderSupplierMaterVo orderSupplierVo, String oid) throws GenericException {
		return supplierOrderDao.listSelectMaterDetailed(pageController, orderSupplierVo, oid);
	}

	@Override
	@Transactional(readOnly = true)
	public PaginationSupport listCustOrderDetailed(PageController pageController,
	        OrderSupplierMaterVo orderSupplierVo, String oid, String supId) throws GenericException {
		return supplierOrderDao.listCustOrderDetailed(pageController, orderSupplierVo, oid, supId);
	}

	public List<Object[]> rowslistCustOrderDetailed(String oid, String supId) throws GenericException {
		return supplierOrderDao.rowslistCustOrderDetailed(oid, supId);
	}

	@Override
	public PaginationSupport listSelectFoodDetailed(PageController pageController,
	        OrderCustFoodVo orderCustFoodVo, String oid) throws GenericException {
		return supplierOrderDao.listSelectFoodDetailed(pageController, orderCustFoodVo, oid);
	}

	/**
	 * 供货商修改材料的品级 业务处理
	 */
	@Override
	public void updateOrderLevel(String ordId, String ordMaterId, String level, String ordSupId)
	        throws GenericException {
		List<OrderSupplierMaterPo> orderSupplierMaterPolist = null;
		orderSupplierMaterPolist = supplierOrderDao.getOrderCust(ordId, ordMaterId, ordSupId);
		for (int i = 0; i < orderSupplierMaterPolist.size(); i++) {
			// 修改供货商材料表的材料重量和
			// //取得相应的等级出净重率
			MaterLevelPo newMaterLevelPo = new MaterLevelPo();
			newMaterLevelPo = supplierOrderDao.getMaterLevelByMaterId(Integer.valueOf(ordMaterId), level);
			// //取出最优的等级为1的净重率
			MaterLevelPo oldMaterLevelPo = new MaterLevelPo();
			oldMaterLevelPo = supplierOrderDao.getMaterLevelByMaterId(Integer.valueOf(ordMaterId), "1");
			// 根据最优的净重率算出当前的实际材料重量
			BigDecimal realityWeight = new BigDecimal("0");
			// zhouziyi --供货商修改材料的品级 业务处理
			// realityWeight = new
			// BigDecimal(orderSupplierMaterPolist.get(i).getOrdMaterWeight())

			// 修改品级
			orderSupplierMaterPolist.get(i).setOrdMaterLevel(Integer.valueOf(level));
			supplierOrderDao.addOrderSupplierMater(orderSupplierMaterPolist.get(i));
		}

	}

}
